package com.lry.basic.algorithm.queue;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/7/10
 */
public class ArrayQueue<T> implements Queue<T> {
    private int size,head,tail;
    private int capacity = 10;
    private T[] queue;

    public ArrayQueue(){
        queue = (T[]) new Object[capacity];
    }
    public ArrayQueue(int capacity){
        this.capacity = capacity;
        queue = (T[]) new Object[capacity];
    }
    @Override
    public T deque() {
        if(size==0)
        return null;
        T temp = queue[head];
        queue[head] = null;
        --size;
        head = (head+1)%capacity;
        return temp;
    }

    @Override
    public boolean enque(T t) {
        if(size==capacity||t==null){
            return false;
        }
        queue[tail] = t;
        ++size;
        tail = (tail+1)%capacity;
        return true;
    }

    @Override
    public int size() {
        return size;
    }
}
